package dianmobile.eistarandroid.ui.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import dianmobile.eistarandroid.R;
import dianmobile.eistarandroid.beans.MailEntity;
import dianmobile.eistarandroid.network.request.RequestFactory;
import dianmobile.eistarandroid.utils.DataUtils;

/**
 * Created by Administrator on 2015/7/21.
 */
public class InMailListAdapter extends BaseAdapter {

    private List<MailEntity> inMailList = new ArrayList<>();
    private LayoutInflater inflater;
    private MailEntity mailItem;

    public InMailListAdapter(Context context, List<MailEntity> list) {
        inMailList = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return inMailList.size();
    }

    @Override
    public Object getItem(int position) {
        return inMailList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        mailItem = inMailList.get(position);

        InMailListAdapter.ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.listitem_in_mail, null);

            viewHolder = new InMailListAdapter.ViewHolder();
            viewHolder.senderName = (TextView)convertView.findViewById(R.id.tv_senderName);
            viewHolder.senderHeader = (CircleImageView)convertView.findViewById(R.id.iv_senderHead);
            viewHolder.inMailTitle = (TextView)convertView.findViewById(R.id.tv_in_mail_title);
            viewHolder.inSendTime = (TextView)convertView.findViewById(R.id.tv_in_sendTime);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
        }

        viewHolder.senderName.setText(mailItem.getUserID());
        viewHolder.inSendTime.setText(mailItem.getSendTime());
        viewHolder.inMailTitle.setText(mailItem.getMailTitle());
        if(mailItem.isRead()) {
            viewHolder.senderName.setAlpha((float)0.54);
        } else {
            viewHolder.senderName.setAlpha((float)1);
        }
        String url = DataUtils.getUserHeadUrlFromName(mailItem.getUserID());
        RequestFactory.makeNetworkImage(viewHolder.senderHeader, url, R.drawable.default_face, R.drawable.default_face);

        return convertView;

    }

    static class ViewHolder {
        TextView senderName;
        TextView inMailTitle;
        TextView inSendTime;
        CircleImageView senderHeader;
    }
}
